package com.xja.wechat.common;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;

public class AccessTokenTest {
    /**
     * 当前方法并不可用--不满足业务需求仅仅测试使用
     *
     * @return
     */
    public static String getAccessToken() {
        String access_Token = "";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6cba9315efa9a68f&secret=701388327f1646ec480e8e15df0f7fcf";
        //指定http请求为get传参方式
        HttpGet httpGet = new HttpGet(url);
        //穿件发送请求的链接
        HttpClient httpClient = new DefaultHttpClient();
        try {
            //执行发送请求 并得到响应结果
            HttpResponse httpResponse = httpClient.execute(httpGet);
            //判断请求是否成功
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String str = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                JSONObject jsonObject = new JSONObject(str);
                access_Token = (String) jsonObject.get("access_token");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return access_Token;
    }

    public static void main(String[] args) {
        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + Constant.getAccessToken();
        //最外边 也就是最大的json
        JSONObject maxJson = new JSONObject();
        //button中存放的一级菜单集合
        JSONArray buttonArray = new JSONArray();
        //第一个一级菜单--最多有三个
        JSONObject menu1 = new JSONObject();
        menu1.put("type", "view");
        menu1.put("name", "首页1");
        String s1 = Constant.snsapi_userinfo("http://www.scorpio-cc.com/app/index.html");
        System.out.println(s1);
        menu1.put("url", s1);
        //第二个一级菜单
        JSONObject menu2 = new JSONObject();
        menu2.put("type", "view");
        menu2.put("name", "购物车1");
        menu2.put("url", Constant.snsapi_base("http://www.scorpio-cc.com/app/shopcar.html"));
        //第三个一级菜单
        JSONObject menu3 = new JSONObject();
        menu3.put("type", "view");
        menu3.put("name", "个人中心1");
        menu3.put("url", Constant.snsapi_userinfo("http://www.scorpio-cc.com/app/pcenter.html"));

        buttonArray.put(menu1);
        buttonArray.put(menu2);
        buttonArray.put(menu3);

        maxJson.put("button", buttonArray);

        HttpPost httpPost = new HttpPost(url);
        HttpClient HttpClient = new DefaultHttpClient();
        //get  post   唯一区别--把需要放入的数据放入到post的Entity中
        httpPost.setEntity(new StringEntity(maxJson.toString(), "utf-8"));

        try {
            HttpResponse httpResponse = HttpClient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String s = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
